//
//  UILabel+xib.swift
//  EPHome
//
//  Created by liujianlong on 2022/8/25.
//

import UIKit
private var borderErrorHexColorKey = "borderErrorHexColorKey"
private var placeErrorHexColorKey = "placeErrorHexColorKey"
private var placeHexColorKey = "placeHexColorKey"
private var errorKey = "errorKey"
private var borderOriginColorKey = "borderOriginColorKey"
private var placeOriginColorKey = "placeOriginColorKey"
extension UITextField {
    @IBInspectable var borderErrorHexColor : String{
        set {
            objc_setAssociatedObject(self, &borderErrorHexColorKey, newValue, .OBJC_ASSOCIATION_COPY_NONATOMIC)
            if self.error {
                self.layer.borderColor = self.borderErrorHexColor.color().cgColor
                self.layer.masksToBounds = true
            }
        }get{
            return objc_getAssociatedObject(self, &borderErrorHexColorKey) as? String ?? "#999999"
        }
    }
    @IBInspectable var placeErrorHexColor : String{
        set{
            objc_setAssociatedObject(self, &placeErrorHexColorKey, newValue, .OBJC_ASSOCIATION_COPY_NONATOMIC)
            if self.error{
                self.attributedPlaceholder = NSAttributedString(string:self.placeholder ?? "", attributes: [.foregroundColor:self.placeErrorHexColor.color()])
            }
        }get{
            return objc_getAssociatedObject(self, &placeErrorHexColorKey) as? String ?? "#999999"
        }
    }
    @IBInspectable var placeHexColor : String{
        set{
            objc_setAssociatedObject(self, &placeHexColorKey, newValue, .OBJC_ASSOCIATION_COPY_NONATOMIC)
            self.attributedPlaceholder = NSAttributedString(string:self.placeholder ?? "", attributes: [.foregroundColor:self.placeHexColor.color()])
            
        }get{
            return objc_getAssociatedObject(self, &placeHexColorKey) as? String ?? "#999999"
        }
    }
    @IBInspectable var error : Bool{
        set{
            objc_setAssociatedObject(self, &errorKey, newValue, .OBJC_ASSOCIATION_ASSIGN)
            if newValue == true{
                self.borderOriginColor = self.borderColor
                self.placeOriginColor = self.placeOriginColor
                self.layer.borderColor = self.borderErrorHexColor.color().cgColor
                self.attributedPlaceholder = NSAttributedString(string:self.placeholder ?? "", attributes: [.foregroundColor:self.placeErrorHexColor.color()])
            }
        }get{
            return objc_getAssociatedObject(self, &errorKey) as? Bool ?? false
        }
    }
    private var borderOriginColor : UIColor{
        set{
            objc_setAssociatedObject(self, &borderOriginColorKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }get{
            return objc_getAssociatedObject(self, &borderOriginColorKey) as? UIColor ?? .black
        }
    }
    private var placeOriginColor : UIColor{
        set{
            objc_setAssociatedObject(self, &placeOriginColorKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }get{
            return objc_getAssociatedObject(self, &placeOriginColorKey) as? UIColor ?? .black
        }
    }
}


